(function($) {
    var arkflow = $.arkflow;

    // 连线
    arkflow.Path = Base.extend({
        constructor : function(config, paper, fromNode, toNode) {
            this.fromNode = fromNode;
            this.toNode = toNode;
            this.paper = paper;

            this.pathConfig = $.extend(true, {}, arkflow.config.path);
            $.extend(true, this.pathConfig, config);

            this.id = "path" + arkflow.util.nextId();
            this.x = 0;
            this.y = 0;

            this.line = new arkflow.PathLine(this);
            this.arrow = new arkflow.PathArrow(this);
            this.linePoints = new arkflow.PathLinePoints(this);
            this.pathText = new arkflow.PathText(this);

            this.drawPath();

            var me = this;
            $([this.line.node, this.arrow.node]).bind("click", function() {
                if (!arkflow.config.editable) {
                    return;
                }
                $(paper).trigger("click", me);
                $(paper).data("currNode", me);
                return false;
            });

            this.bindRemoveRect();
            this.bindRectResize();
        },
        // 监听删除节点的时候删除路径
        bindRemoveRect : function() {
            var me = this;
            var removeRect = function(event, rect) {
                if (!arkflow.config.editable) {
                    return;
                }
                if (rect && (rect.getId() == me.fromNode.getId() || rect.getId() == me.toNode.getId())) {
                    $(me.paper).trigger("removepath", me);
                }
            };
            $(this.paper).bind("removerect", removeRect);
        },
        // 监听节点改变大小的时候重新画线
        bindRectResize : function() {
            var me = this;
            var rectreSize = function(event, node) {
                if (!arkflow.config.editable) {
                    return;
                }
                if (me.fromNode && me.fromNode.getId() == node.getId()) {
                    var o;
                    if (me.linePoints.fromDot().right().right().type() == "to") {
                        o = {
                            x : me.toNode.getBBox().x + me.toNode.getBBox().width / 2,
                            y : me.toNode.getBBox().y + me.toNode.getBBox().height / 2
                        };
                    } else {
                        o = me.linePoints.fromDot().right().right().pos();
                    }
                    var r = arkflow.util.connPoint(me.fromNode.getBBox(), o);
                    me.linePoints.fromDot().moveTo(r.x, r.y);
                    me.drawPath();
                }
                if (me.toNode && me.toNode.getId() == node.getId()) {
                    var o;
                    if (me.linePoints.toDot().left().left().type() == "from") {
                        o = {
                            x : me.fromNode.getBBox().x + me.fromNode.getBBox().width / 2,
                            y : me.fromNode.getBBox().y + me.fromNode.getBBox().height / 2
                        };
                    } else {
                        o = me.linePoints.toDot().left().left().pos();
                    }
                    var r = arkflow.util.connPoint(me.toNode.getBBox(), o);
                    me.linePoints.toDot().moveTo(r.x, r.y);
                    me.drawPath();
                }
            };
            $(this.paper).bind("rectresize", rectreSize);
        },

        drawPath : function() {
            this.line.draw();
            this.arrow.draw();
            this.pathText.draw();
        },
        getId : function() {
            return this.id;
        },
        text : function() {
            return this.pathText.getText();
        },
        attr : function(o) {
            if (o && o.path) {
                this.line.attr(o.path);
            }
            if (o && o.arrow) {
                this.arrow.attr(o.arrow);
            }
        },
        remove : function() {
            this.linePoints.remove();
            this.line.remove();
            this.arrow.remove();
            this.pathText.remove();
            try {
                $(this.paper).unbind("click", l);
            } catch (o) {
            }
            try {
                $(this.paper).unbind("removerect", A);
            } catch (o) {
            }
            try {
                $(this.paper).unbind("rectresize", d);
            } catch (o) {
            }

        },
        from : function() {
            return this.fromNode;
        },
        to : function() {
            return this.toNode;
        },
        toJson : function() {
            var result = "{from:'" + this.fromNode.getId() + "',to:'" + this.toNode.getId() + "', dots:"
                + this.linePoints.toJson() + ",text:{text:'"
                + this.pathText.getText() + "'},textPos:{x:"
                + Math.round(this.pathConfig.textPos.x)
                + ",y:" + Math.round(this.pathConfig.textPos.y) + "}, props:{";
            for (var propName in this.pathConfig.props) {
                result += propName + ":{value:'" + this.pathConfig.props[propName].value + "'},";
            }
            if (result.substring(result.length - 1, result.length) == ",") {
                result = result.substring(0, result.length - 1);
            }
            result += "}}";
            return result;
        },
        restore : function(o) {
            this.pathConfig = $.extend(true, this.pathConfig, o);
            this.linePoints.restore(o.dots);
        }
    });

})(jQuery);
